package id.pratama.pantaijogja;

import java.io.File;
import java.util.List;

import org.json.JSONObject;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import id.pratama.pantaijogja.entity.Pantai;
import id.pratama.pantaijogja.util.CustomAdapter;
import id.pratama.pantaijogja.util.JSONHelper;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity implements
		OnItemClickListener
{
	private static final String			API_URL		= "http://api.pratamawijaya.com/pantai_jogja/api.php";
	private JSONHelper					json;
	private CustomAdapter				adapter;
	private ProgressDialog				pDialog;
	private ImageLoader					imageLoader;
	private ImageLoaderConfiguration	config;
	private DisplayImageOptions			options;

	private AdView						ads;
	private ListView					lv;

	public static final String			TAG_LOKASI	= "lokasi";
	public static final String			TAG_GAMBAR	= "gambar";
	public static final String			TAG_DESC	= "deskripsi";
	public static final String			TAG_LAT		= "lat";
	public static final String			TAG_LNG		= "lng";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = (ListView) findViewById(R.id.lv_menu);
		ads = (AdView) findViewById(R.id.ads);

		lv.setOnItemClickListener(this);

		getSupportActionBar().setIcon(android.R.color.transparent);

		// ads
		AdRequest adRequest = new AdRequest();
		adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

		ads.loadAd(adRequest);

		json = new JSONHelper();

		// imagecache
		File cacheDir = StorageUtils.getCacheDirectory(this);

		imageLoader = ImageLoader.getInstance();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_launcher).cacheOnDisc(true)
				.delayBeforeLoading(100).cacheInMemory(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();

		config = new ImageLoaderConfiguration.Builder(this)
				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
				.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
				.writeDebugLogs().build();

		imageLoader.init(config);
		new AsyncData().execute(API_URL);
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		if (ads != null)
		{
			ads.destroy();

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	private class AsyncData extends AsyncTask<String, Void, List<Pantai>>
	{
		@Override
		protected List<Pantai> doInBackground(String... arg0)
		{
			// TODO Auto-generated method stub
			JSONObject obj = json.getJSONFromURL(arg0[0]);

			return json.getAllLocation(obj);
		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();
			pDialog = new ProgressDialog(MainActivity.this);
			pDialog.setTitle("Loading Data..");
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected void onPostExecute(List<Pantai> result)
		{
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			pDialog.dismiss();
			adapter = new CustomAdapter(MainActivity.this, imageLoader,
					options, result);
			lv.setAdapter(adapter);

		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id)
	{
		Bundle bundle = new Bundle();
		bundle.putString(TAG_LOKASI,
				((Pantai) adapter.getItem(position)).getNama());
		bundle.putString(TAG_DESC,
				((Pantai) adapter.getItem(position)).getDeskripsi());
		bundle.putString(TAG_GAMBAR,
				((Pantai) adapter.getItem(position)).getGambar());
		bundle.putDouble(TAG_LAT, ((Pantai) adapter.getItem(position)).getLat());
		bundle.putDouble(TAG_LNG, ((Pantai) adapter.getItem(position)).getLng());

		Intent i = new Intent(this, LokasiActivity.class);
		i.putExtras(bundle);
		startActivity(i);

	}
}
